﻿package com.wandian.view.mediator.cornucopia
{
	import com.wandian.controller.AccountCommand;
	import com.wandian.controller.CLZNoticeName;
	import com.wandian.controller.CornucopiaCommand;
	import com.wandian.data.GameData;
	import com.wandian.model.InRoomPlayerDataProxy;
	import com.wandian.model.SwfDataProxy;
	import com.wandian.model.vo.PlayerVo;
	import com.wandian.model.vo.RoomVo;
	import com.wandian.util.CLZButton;
	import com.wandian.util.CLZListPage;
	import com.wandian.util.CLZListPageGetPage;
	import com.wandian.util.CLZLlistScrollPanel;
	import com.wandian.util.CLZTabBar;
	import com.wandian.util.GlowFilterManage;
	import com.wandian.util.GolbalTimer;
	import com.wandian.view.components.ui.AddBtn;
	import com.wandian.view.components.ui.CardManage;
	import com.wandian.view.components.ui.MaskTipPanel;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	import org.puremvc.as3.interfaces.*;
	import org.puremvc.as3.patterns.mediator.*;

    public class CornucopiaMediator extends Mediator
    {
        private var _tabBar:CLZTabBar;
        private var _mcPanel:MovieClip;
        private var _mcConten:Sprite;
        private var _global:GameData;
        public static const NAME:String = "CornucopiaMediator";

        public function CornucopiaMediator(param1:Object = null)
        {
            this._mcConten = new Sprite();
            this._global = GameData.getInstance();
            super(NAME, param1);
            return;
        }

        override public function onRegister() : void
        {
            this.mcCornucopia["title_txt"].text = GameData.tip["CORNUCOPIA_MESSAGE"];
            AddBtn.initBtn(this.mcCornucopia, CLZNoticeName.HIDE_CORNUCOPIA_PANEL, AddBtn.CLOSE_TWO, "", 4, 1);
            facade.registerCommand(CLZNoticeName.CORNUCOPIA_TODAY_LIST_REQ, CornucopiaCommand);
            facade.registerCommand(CLZNoticeName.CORNUCOPIA_MY_LIST_REQ, CornucopiaCommand);
            this.mcCornucopia.addChild(this._mcConten);
            this._mcConten.x = 21.4;
            this._mcConten.y = 87;
            var tabs:Array = [[this.mcCornucopia.toDay_btn, GameData.tip["TO_DAY_REWARD"]], [this.mcCornucopia.record_btn, GameData.tip["MY_RECORD"]], [this.mcCornucopia.intro_btn, GameData.tip["INTRO_CORNUCOPIA"]]];
            this._tabBar = new CLZTabBar(tabs);
            var index:int = 0;
            while (index < tabs.length)
            {
                
                tabs[index][0].addEventListener(Event.CHANGE, this.changeTabBarHandler);
                index = index + 1;
            }
            this._tabBar.setClickIndex(this.mcCornucopia.intro_btn);
            return;
        }

        private function changeTabBarHandler(event:Event) : void
        {
            var _loc_2:Array = null;
            var _loc_3:int = 0;
            GameData.removeNumChildre(this._mcConten);
            switch(event.target.name)
            {
                case "toDay_btn":
                {
                    this.gotoPageFun(1);
                    break;
                }
                case "record_btn":
                {
                    this.sendNotification(CLZNoticeName.CORNUCOPIA_MY_LIST_REQ);
                    break;
                }
                case "intro_btn":
                {
                    this._mcPanel = GameData.getMovieClip("CornucopiaIntro");
                    this._mcPanel.intro_txt.htmlText = GameData.tip["CORNUCOPIA_INTRO_CONTENT"];
                    this._mcPanel.name0.text = GameData.tip["CORNUCOPIA_INTRO_NAME0"];
                    this._mcPanel.name1.text = GameData.tip["CORNUCOPIA_INTRO_NAME1"];
                    this._mcPanel.name2.text = GameData.tip["CORNUCOPIA_INTRO_NAME2"];
                    this._mcPanel.name3.text = GameData.tip["CORNUCOPIA_INTRO_NAME3"];
                    this._mcPanel.name4.text = GameData.tip["CORNUCOPIA_INTRO_NAME4"];
                    this._mcPanel.name5.text = GameData.tip["CORNUCOPIA_INTRO_NAME5"];
                    this._mcPanel.name6.text = GameData.tip["CORNUCOPIA_INTRO_NAME6"];
                    _loc_2 = GameData.tip["cardType"].split("|");
                    _loc_3 = _loc_2.length;
                    this._mcPanel.cardType0.text = _loc_2[(_loc_3 - 1)];
                    this._mcPanel.cardType1.text = _loc_2[_loc_3 - 2];
                    this._mcPanel.cardType2.text = _loc_2[_loc_3 - 3];
                    this._mcConten.addChild(this._mcPanel);
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }

        /**
         * 请求今日积宝
         * @param mc
         * @param data
         */
        private function showTodayList(mc:MovieClip, data:Object) : void
        {
            this._mcConten.addChild(mc);
            mc["name_txt"].text = GameData.tip["CORNUCOPIA_LIST_NAME"];//中奖玩家
            mc["gold_txt"].text = GameData.tip["CORNUCOPIA_LIST_GOLD"];
            mc["cardType_txt"].text = GameData.tip["CORNUCOPIA_LIST_CARDS"];
            mc["time_txt"].text = GameData.tip["CORNUCOPIA_LIST_TIME"];
            mc["num_txt"].text = GameData.tip["CORNUCOPIA_LIST_NUM"];
            var rows:Array = [mc.info1, mc.info2, mc.info3, mc.info4, mc.info5, mc.info6, mc.info7, mc.info8, mc.info9, mc.info10, mc.info11];
            CLZButton.setButton(mc.prev_btn, GameData.tip["PREV_PAGE"]);
            CLZButton.setButton(mc.next_btn, GameData.tip["NEXT_PAGE"]);
            CLZButton.setButton(mc.go_btn, "GO");
            var index:int = 0;
            while (index < rows.length)
            {
                rows[index].addEventListener(CLZListPage.UPDATE_DATA, this.updateInfoDataHandler);
                index = index + 1;
            }
            var mcPageList:CLZListPageGetPage = new CLZListPageGetPage(rows, mc.prev_btn, mc.next_btn, mc._txtPaging, mc._txtInput, mc.go_btn, this.gotoPageFun);
			//arrRowData{arr:[[name_txt,money_txt,arrCards,time_txt,num_txt],[]],page:1,all:20}
            mcPageList.setInfoPage(data.arr, data.page, data.all);
            return;
        }

        private function gotoPageFun(pageIndex:int) : void
        {
            this.sendNotification(CLZNoticeName.CORNUCOPIA_TODAY_LIST_REQ, pageIndex);
            return;
        }

        /**
         * 显示5个扑克牌
         * @param event
         */
        private function updateInfoDataHandler(event:Event) : void
        {
            var cardManage:CardManage = null;
            var mcRow:MovieClip = event.target as MovieClip;
            var rowInfo:Object = mcRow.info;
            mcRow.name_txt.text = rowInfo[1];
            mcRow.money_txt.text = rowInfo[4];
            mcRow.time_txt.text = GameData.setDate(rowInfo[5]);
            mcRow.num_txt.text = rowInfo[6] + "";
            mcRow.share_txt.htmlText = "";
            mcRow.share_txt.addEventListener(TextEvent.LINK, this.linkTxtHandler);
            var arrCards:Array = rowInfo[2].split(",");
            var index:int = 0;
            while (index < 5)
            {
                
                cardManage = new CardManage(GameData.getMovieClip("Card"));
                cardManage.showCard(arrCards[index], false);
                cardManage.width = 23;
                cardManage.height = 30;
                cardManage.x = index * cardManage.width + index * 2 + 295;
                mcRow.addChild(cardManage);
                index = index + 1;
            }
            return;
        }

        override public function listNotificationInterests() : Array
        {
            return [CLZNoticeName.HIDE_CORNUCOPIA_PANEL, CLZNoticeName.CORNUCOPIA_TODAY_LIST_RES, CLZNoticeName.CORNUCOPIA_MY_LIST_RES];
        }

        /**
         * 
         * @param notification
         */
        /**
         * 
         * @param notification
         */
        override public function handleNotification(notification:INotification) : void
        {
            var arrMyCornucopia:Array = null;
            var myListScroll:CLZLlistScrollPanel = null;
            var mcMyCornucopiaList:MovieClip = null;
            var total:int = 0;
            var index:int = 0;
            var myRowInfo:Array = null;
            var mcRow:MovieClip = null;
            var arrRowCard:Array = null;
            var cardManage:CardManage = null;
            var index2:int = 0;
            switch(notification.getName())
            {
                case CLZNoticeName.HIDE_CORNUCOPIA_PANEL:
                {
                    facade.removeMediator(CornucopiaMediator.NAME);
                    facade.removeCommand(CLZNoticeName.CORNUCOPIA_TODAY_LIST_REQ);
                    facade.removeCommand(CLZNoticeName.CORNUCOPIA_MY_LIST_REQ);
                    if (this.mcCornucopia != null)
                    {
                    }
                    if (Texas.I._mcWindosView.contains(this.mcCornucopia))
                    {
                        Texas.I._mcWindosView.removeChildTo(this.mcCornucopia);
                        viewComponent = null;
                    }
                    break;
                }
                case CLZNoticeName.CORNUCOPIA_TODAY_LIST_RES:
                {
                    GameData.removeNumChildre(this._mcConten);
                    this._mcPanel = GameData.getMovieClip("CornucopiaList");
                    this.showTodayList(this._mcPanel, notification.getBody());
                    break;
                }
                case CLZNoticeName.CORNUCOPIA_MY_LIST_RES:
                {
                    GameData.removeNumChildre(this._mcConten);
                    arrMyCornucopia = notification.getBody() as Array;
                    myListScroll = new CLZLlistScrollPanel(1, 743, 32, 0, 2, 408, 732);
                    mcMyCornucopiaList = GameData.getMovieClip("CornucopiaMyList");
                    this._mcConten.addChild(mcMyCornucopiaList);
                    mcMyCornucopiaList.addChild(myListScroll);
                    myListScroll.x = 1;
                    myListScroll.y = 30;
                    total = arrMyCornucopia.length;
                    index = 0;
                    while (index < total)
                    {
                        myRowInfo = arrMyCornucopia[index];
                        mcRow = GameData.getMovieClip("CornucopiaPanelInfoMc");
                        myListScroll.setMc(mcRow, index, total);
                        mcRow.name_txt.text = this._global.userInfo.userVo.name;
                        mcRow.money_txt.text = myRowInfo[2];
                        mcRow.time_txt.text = GameData.setDate(myRowInfo[3]);
                        mcRow.num_txt.text = "1";
                        mcRow.share_txt.htmlText = "<a href=\"event:" + this._global.userInfo.userVo.userid + "_" + myRowInfo[0] + "\"><u><font color=\'#ffff00\'>" + GameData.tip["CORNUCOPIA_LIST_SHARE_ME"] + "</font></u></a>";
                        mcRow.share_txt.addEventListener(TextEvent.LINK, this.linkTxtHandler);
                        arrRowCard = myRowInfo[0].split(",");
                        index2 = 0;
                        while (index2 < 5)
                        {
                            
                            cardManage = new CardManage(GameData.getMovieClip("Card"));
                            cardManage.showCard(arrRowCard[index2], false);
                            cardManage.width = 23;
                            cardManage.height = 30;
                            cardManage.x = index2 * cardManage.width + index2 * 2 + 295;
                            cardManage.y = 1;
                            mcRow.addChild(cardManage);
                            index2 = index2 + 1;
                        }
                        index = index + 1;
                    }
                    this._mcConten.addChild(myListScroll);
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }

        private function linkTxtHandler(event:TextEvent) : void
        {
            var _loc_2:* = event.text;
            var _loc_3:* = _loc_2.split("_");
//            JSFUN.cornucopiaFunc(_loc_3[0], _loc_3[1]);
            return;
        }

        private function get mcCornucopia() : MovieClip
        {
            return viewComponent as MovieClip;
        }

    }
}
